<?php
/**
 * Overview of information about an order and its phases
 * @author Andy Rich <andy.rich@gmail.com>
 * @version 1.0
 * @since 2013-11-12
 */
require_once "database.php";
require_once 'order.php';
require_once 'customer.php';
require_once 'phase.php';
require_once 'machines.php';

loginInfo::update_login ();

?>

<html>
	<head>
		<title>Opol - Order View</title>
	</head>
	<body>
		<link rel="stylesheet" type="text/css" href="opol.css">
		<?php require 'menu.php' ?>

<?php if (!$_GET) : ?>
	<form name="text" action="view_order.php" method="get">
	<label>Order Numer: </label>
	<input name="order" type="text" size="10"> <br>
	<label>&nbsp;</label>
	<input type="submit" value="Search"> <br>
	</form>
<?php
else :
	$db = new database ();
	$id = $_GET["order"];
	$order = new order ($id);
	$order->init ($db);

	$customer = new customer ($order->customer);
	$customer->init($db);

	$ship_addr 	= $order->shipping_addr;
	$hold 		= $order->hold;
	$pri 		= $order->priority;
	$cost 		= $order->price;
	$instr 		= $order->special_instructions;
	$due 		= $order->deadline;
	$qty 		= $order->quantity;

	$cust_name 	= $customer->name;
	$cust_email = $customer->email;
	$cust_phone = $customer->phone_num;
	$cust_addr 	= $customer->address;

	$cust_ac 	= (int)($cust_phone / 10000000);
	$cust_three = (int)($cust_phone / 10000);
	$cust_last 	= (int)($cust_phone - ($cust_three * 10000));
	$cust_three = $cust_ac * 1000 - $cust_three;
  
	print "<h1>Info for order $id</h1><br>";
	print "<b>Customer:</b> $cust_name<br>";
	printf ("<b>Phone:</b> (%03d)%03d-%04d<br>", $cust_ac, $cust_three, $cust_last);
	print "<b>Email:</b> $cust_email<br>";
	print "<b>Address:</b> $cust_addr<br><br>";
	print "<b>Due:</b> $due<br>";
	print "<b>Quantity:</b> $qty<br>";
	print "<b>Cost:</b> \$$cost<br>";
	if ($hold){
		print "<b>This order has a hold</b><br>";
	}
	print "<b> Priority:</b> ";
	print order::$priorities[$pri];
  
	print "<br>";
	if ($instr){
		print "<b>Instructions:</b> $instr<br>";
	}
  
	print "<b> Ship To:</b> $ship_addr<br>";

	$phase_info = $order->collect_phases ($db);

	print "<br><br><h2>Phase Information</h2>";
	print "<table border=1>";
	print "<tr><th>Step</th><th>Machine</th><th>Time</th><th>Keyword</th></tr>";
	for ($i = 0; isset ($phase_info[$i]); $i++) {
		$pri 		= $phase_info[$i]->priority;
		$mach 		= $phase_info[$i]->machId;
		$keyword 	= $phase_info[$i]->keyword;
		$time 		= $phase_info[$i]->time_req;
		$minutes 	= $time;
		$hours 		= (int)($minutes / 60);
		$days 		= (int)($hours / 24);
		$time_str 	= sprintf (
				"%d d, %d h, %d m",
				(int)($days),
				(int)($hours % 24),
				(int)($minutes % 60)
			);

		$m = new machine ($mach);
		$m->init($db);
		$m_name = $m->name;
		print "<tr><td>$pri</td><td>$m_name</td><td>$time_str</td><td>$keyword</td></tr>";
	}
	print "</table>";
?>

<?php endif; ?>

	</body>
</html>